package com.zz.array;

import com.zz.node.ListNode;
import com.zz.util.ListNodeUtil;
import com.zz.util.NodeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author 张哲
 * @email a252013135@163.com
 * @date 2024/6/10 16:02
 * 书店店员有一张链表形式的书单，每个节点代表一本书
 * 节点中的值表示书的编号。为更方便整理书架，店员需要
 * 将书单倒过来排列，就可以从最后一本书开始整理，逐一
 * 将书放回到书架上。请倒序返回这个书单链表。
 * 示例 1：
 * 输入：head = [3,6,4,1]
 * 输出：[1,4,6,3]
 * 0 <= 链表长度 <= 10000
 *
 */
public class LCR125Solution {
    List<Integer> integerList = new ArrayList();
    public int[] reverseBookList1(ListNode head) {
        if(head == null){
            return new int[0];
        }
        Stack<Integer> stack = new Stack<Integer>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int[] arr = new int[stack.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = stack.pop();
        }
        return arr;
    }

    public int[] reverseBookList2(ListNode head) {
        if(head == null){
            return new int[0];
        }
        List<Integer> list = new ArrayList();
        addListBook(list, head);
        int[] arr = new int[list.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    public List<Integer> addListBook(List list, ListNode head) {
        if(head == null){
            return list;
        }
        addListBook(list, head.next);
        list.add(head.val);
        return list;
    }

    public int[] reverseBookList3(ListNode head) {
        //先反转链表
        ListNode node = ListNodeUtil.reverseListNode(head);
        List<Integer> list = new ArrayList<Integer>();
        while (node != null){
            list.add(node.val);
            node = node.next;
        }
        //然后在转换为数组
        int[] arr = new int[list.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }
}
